package com.mzy.interview

import kotlin.math.max
import kotlin.math.min

/**
 * @Author: mizy3
 * @Date: 2024/08/25/下午3:39
 * @Description:
 */
class 买卖股票I {
    /*  fun maxProfit(prices: IntArray): Int {
          // dp[i][0]表示第i天持有股票
          // dp[i][1]表示第i天卖出股票
          val dp = Array<Array<Int>>(prices.size) { Array(2) { 0 } }

          dp[0][0] = prices[0]
          dp[0][1] = 0

          // dp[i][0] = Math.min(dp[i - 1][0], prices[i])
          // dp[i][1] = Math.max(prices[i] - dp[i - 1][0], dp[i - 1][1])
          for (i in 1..<prices.size) {
              dp[i][0] = min(dp[i - 1][0], prices[i])
              dp[i][1] = max(prices[i] - dp[i - 1][0], dp[i - 1][1])
              println("${dp[i][0]}  ${dp[i][1]}")
          }

          return dp[dp.size - 1][1]
      }*/

    fun maxProfit(prices: IntArray): Int {
        var minVal = Int.MAX_VALUE
        var maxVal = Int.MIN_VALUE
        for (i in 0..<prices.size) {
            minVal = min(prices[i], minVal)
            maxVal = max(prices[i] - minVal, maxVal)
        }

        return maxVal
    }
}

fun main() {
    println(买卖股票I().maxProfit(arrayOf(7, 1, 5, 3, 6, 4).toIntArray()))
}